package utils
{
	import flash.utils.Dictionary;

	/**
	 * 金币转化 表
	 * @author xumin.xu
	 *
	 */
	public class ConversionTable
	{
		public function ConversionTable()
		{

		}

		public static var conversionTableDic:Dictionary = new Dictionary();

		public static function init(conversionTable:*):void
		{
			var item:XML;
			for each (item in conversionTable.Exchange)
			{
				var code1:String = item.Code1;
				var code2:String = item.Code2;
				var rate:Number = item.Rate;

				conversionTableDic[code1 + "_" + code2] = rate;
				conversionTableDic[code2 + "_" + code1] = 1 / rate;
			}
		}

		/**
		 * 获取币种标志
		 * @param type 币种
		 */
		public static function getMoneySign(type:String):String
		{
			return type;
		}

		/**
		 * 转换为type2币种的金币值
		 * @param money 需要转换的金币数量
		 * @param type1 需要转换的币种
		 * @param type2 目的币种
		 */
		public static function conversionMoney(money:Number, type1:String, type2:String):Number
		{
			var rate:Number = conversionTableDic[type1 + "_" + type2];

			if(type1 == type2)
				rate = 1;
			
			var money1:Number = money * rate;

			return FloatHandler.TwoFormat(money1);
		}

		/**
		 * 以type2的币种形式显示
		 * @param money 需要转换的金币数量
		 * @param type1 需要转换的币种
		 * @param type2 目的币种
		 */
		public static function moneyToString(money:Number, type1:String, type2:String):String
		{
			var money1:Number = conversionMoney(money, type1, type2);
			var showMoney:String = "" + type2 + FloatHandler.TwoFormatToString(money1);
			return showMoney;
		}
	}
}
